package com.webdrider.servlet.action.blog;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.security.exceptions.ManagerException;
import com.common.security.exceptions.ParserException;
import com.common.servlet.abstracts.AbstractJSONParser;
import com.common.servlet.abstracts.AbstractSimpleServlet;
import com.webdrider.manager.action.blog.CategoryManager;
import com.webdrider.manager.interfaces.ICategoryManager;
import com.webdrider.model.response.blog.GetAllCategoriesResponse;
import com.webdrider.servlet.util.RequestAction;
import com.webdrider.servlet.util.parser.blog.GetAllCategoriesJSONParser;

public class GetAllCategoriesServlet extends AbstractSimpleServlet {
	private static final long serialVersionUID = 1L;

	private ICategoryManager categoryManager = new CategoryManager();
	private GetAllCategoriesJSONParser parser = new GetAllCategoriesJSONParser();
	
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response) throws ManagerException, IOException {
		try {
			GetAllCategoriesResponse allCategoriesResponse = new GetAllCategoriesResponse();
			allCategoriesResponse.setCategories(categoryManager.getAll());
			allCategoriesResponse.setRequestAction(RequestAction.GET_ALL_CATEGORIES);
			
			response.setContentType("json");
			response.getWriter().write(parser.parseResponse(allCategoriesResponse));
			response.getWriter().close();
		} catch (ParserException parserException) {
			exception(parserException, response);
		}
	}

	@Override
	public AbstractJSONParser parser() {
		return parser();
	}
}
